﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace BanXeTang
{
    public class TankDichLoai1 : TankDich
    {
        public override VisibleGameEntity Clone()
        {
            VisibleGameEntity newObject = new TankDichLoai1();
            newObject._nsprite = this._nsprite;
            newObject._sprite = new MySprite[newObject._nsprite];
            for (int i = 0; i < newObject._nsprite; i++)
            {
                newObject._sprite[i] = new MySprite(this._sprite[i].texture2d, X, Y);
            }
            ((TankDichLoai1)newObject).nDelay = this.nDelay;
            ((TankDichLoai1)newObject).iDelay = 0;
            ((TankDichLoai1)newObject).tocDo = tocDo;
            ((TankDichLoai1)newObject).tamKiemSoat = tamKiemSoat;
            ((TankDichLoai1)newObject).nDoTre = nDoTre;
            ((TankDichLoai1)newObject).maxMau = maxMau;
            ((TankDichLoai1)newObject).mau = maxMau;
            ((TankDichLoai1)newObject).thoiGianTatTimDiet = thoiGianTatTimDiet;
            ((TankDichLoai1)newObject).iThoiGianTatTimDiet = thoiGianTatTimDiet;

            return newObject;
        }

        public override void Update(GameTime gameTime)
        {
            if (Math.Abs(X - Game1.tankTa.X) < tamKiemSoat && Math.Abs(Y - Game1.tankTa.Y) < tamKiemSoat)
            {
                timThay = true;
            }

            //viên đạn tiếp tục bay tiếp
            _dan.Update(gameTime);
            
             
            

            //Nếu đang tìm diệt mà phải quay đầu thì ko cho tìm diệt nữa
            if (phaiQuayDau && timThay)
            {
                iThoiGianTatTimDiet = 0;
                timDiet = false;
            }

            //Khi đã phát hiện ra xe tăng của mình
            if (timThay && timDiet)
            {
                if (X == Game1.tankTa.X)
                {
                    if (Y < Game1.tankTa.Y)
                        huong = 2;
                    if (Y > Game1.tankTa.Y)
                        huong = 0;
                    if (!_dan.duocBan && tiepTucBan)
                        Ban();
                }
                if (Math.Abs(Y - Game1.tankTa.Y) <= 15)
                {
                    if (X < Game1.tankTa.X)
                        huong = 1;
                    if (X > Game1.tankTa.X)
                        huong = 3;
                    if (!_dan.duocBan && tiepTucBan)
                        Ban();

                }
                
                if (iDoTre == 0)
                    tiepTucBan = true;
                iDoTre = (iDoTre + 1) % nDoTre;

                if (X < Game1.tankTa.X)
                {
                    huong = 1;
                    if (X + tocDo > Game1.tankTa.X)
                        X = Game1.tankTa.X;
                    else
                        X += tocDo;
                    if (iDelay == 0)
                        _sprite[huong].Update(gameTime);
                    iDelay = (iDelay + 1) % nDelay;

                    return;
                }
                if (X > Game1.tankTa.X)
                {
                    huong = 3;
                    if (X - tocDo < Game1.tankTa.X)
                        X = Game1.tankTa.X;
                    else
                        X -= tocDo;
                    if (iDelay == 0)
                        _sprite[huong].Update(gameTime);
                    iDelay = (iDelay + 1) % nDelay;

                    return;
                }

                if (Y < Game1.tankTa.Y)
                {
                    huong = 2;
                    if (Y + tocDo > Game1.tankTa.Y)
                        Y = Game1.tankTa.Y;
                    else
                        Y += tocDo;
                    if (iDelay == 0)
                        _sprite[huong].Update(gameTime);
                    iDelay = (iDelay + 1) % nDelay;

                    return;
                }
                if (Y > Game1.tankTa.Y)
                {
                    huong = 0;
                    if (Y - tocDo < Game1.tankTa.Y)
                        Y = Game1.tankTa.Y;
                    else
                        Y -= tocDo;
                    if (iDelay == 0)
                        _sprite[huong].Update(gameTime);
                    iDelay = (iDelay + 1) % nDelay;

                    return;
                }
            }
            else
            {
                if (phaiQuayDau)
                {
                    switch (huong)
                    {
                        case 0:
                            {
                                huong = 2;
                                break;
                            }
                        case 1:
                            {
                                huong = 3;
                                break;
                            }
                        case 2:
                            {
                                huong = 0;
                                break;
                            }
                        case 3:
                            {
                                huong = 1;
                                break;
                            }
                    }
                }
                else
                {
                    Random rd = new Random();
                    if (iDoiHuong == 0)
                        huong = rd.Next(0, 3);
                }
                switch (huong)
                {
                    case 0:
                        {
                            Y = Y - tocDo;
                            if (Y < 0)
                            {
                                Y = 0;
                                huong = 2;
                            }

                            break;
                        }
                    case 1:
                        {
                            X = X + tocDo;
                            if (X > GlobalSettings._curMapWidth - _sprite[huong].texture2d[0].Width)
                            {
                                X = GlobalSettings._curMapWidth - _sprite[huong].texture2d[0].Width;
                                huong = 3;
                            }
                            break;
                        }
                    case 2:
                        {
                            Y = Y + tocDo;
                            if (Y > GlobalSettings._curMapHeight - _sprite[huong].texture2d[0].Height)
                            {
                                Y = GlobalSettings._curMapHeight - _sprite[huong].texture2d[0].Height;
                                huong = 0;
                            }
                            break;
                        }
                    case 3:
                        {
                            X = X - tocDo;
                            if (X < 0)
                            {
                                X = 0;
                                huong = 1;
                            }
                            break;
                        }
                }
                iDoiHuong = (iDoiHuong + 1) % nDoiHuong;
                if (iDelay == 0)
                    _sprite[huong].Update(gameTime);
                iDelay = (iDelay + 1) % nDelay;

                if(iThoiGianTatTimDiet < thoiGianTatTimDiet)
                    iThoiGianTatTimDiet++;
                else
                {
                    timDiet = true;
                }
                
                //Trả biến phaiQuayDau về false
                phaiQuayDau = false;
            }

        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            _sprite[huong].Draw(gameTime, spriteBatch, Color.Yellow);
            _dan.Draw(gameTime, spriteBatch);
        }

        public override int DiemCong()
        {
            return 10;
        }

        
    }
}
